package com.newshub.app.ui.favorite

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.newshub.app.data.model.News
import com.newshub.app.databinding.FragmentFavoriteBinding
import com.newshub.app.ui.news.NewsDetailActivity
import com.newshub.app.ui.adapter.NewsAdapter
import com.newshub.app.ui.favorite.FavoriteViewModel

class FavoriteFragment : Fragment() {
    
    private var _binding: FragmentFavoriteBinding? = null
    private val binding get() = _binding!!
    
    private lateinit var viewModel: FavoriteViewModel
    private lateinit var newsAdapter: NewsAdapter
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentFavoriteBinding.inflate(inflater, container, false)
        return binding.root
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        initViewModel()
        initRecyclerView()
        loadFavorites()
    }
    
    private fun initViewModel() {
        viewModel = ViewModelProvider(this)[FavoriteViewModel::class.java]
        
        // 观察收藏的新闻列表
        viewModel.favoriteNews.observe(viewLifecycleOwner) { newsList ->
            newsAdapter.submitList(newsList)
            
            // 显示或隐藏空状态
            binding.emptyStateTextView.visibility = if (newsList.isEmpty()) View.VISIBLE else View.GONE
        }
        
        // 观察加载状态
        viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
            binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
        }
        
        // 观察错误信息
        viewModel.errorMessage.observe(viewLifecycleOwner) { errorMessage ->
            if (errorMessage != null) {
                Toast.makeText(requireContext(), errorMessage, Toast.LENGTH_SHORT).show()
            }
        }
    }
    
    private fun initRecyclerView() {
        newsAdapter = NewsAdapter(
            onItemClick = { news ->
                openNewsDetail(news.docid)
            },
            onFavoriteClick = { news ->
                toggleFavorite(news)
            }
        )
        
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = newsAdapter
        }
    }
    
    private fun loadFavorites() {
        viewModel.loadFavoriteNews()
    }
    
    private fun openNewsDetail(newsId: String) {
        val intent = Intent(requireContext(), NewsDetailActivity::class.java)
        intent.putExtra("news_id", newsId)
        startActivity(intent)
    }
    
    private fun toggleFavorite(news: News) {
        viewModel.toggleFavorite(news)
    }
    
    override fun onResume() {
        super.onResume()
        // 每次回到页面时刷新数据
        loadFavorites()
    }
    
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}